package br.edu.com1263.dao;

import java.util.Collection;

import javax.persistence.Query;

import br.edu.com1263.model.Avalia;

public class AvaliaDAOImpl extends AbstractDAO implements AvaliaDAO{
	
	public AvaliaDAOImpl(){

}

	@Override
	public Collection<Avalia> consultarTodos() {
		Query query = manager.createQuery("SELECT a FROM Avalia a");
		return query.getResultList();
	}

	@Override
	public void salvar(Avalia avalia) {
		manager.persist(avalia);
		
	}
	
	
	public void alterar(Avalia avalia) {
		manager.merge(avalia);
	}
    
    public void remover(Avalia avalia) {
		manager.remove(avalia);
	}

	@Override
	public Avalia consultar(Long id) {
		return manager.find(Avalia.class, id);
	}



	@Override
	public Collection<Avalia> consultarPostado(Long idPostado) {
		Query query = manager.createQuery("SELECT a FROM Avalia a WHERE a.postado.id = :idPostado ");
		query.setParameter("idPostado", idPostado);

		return query.getResultList();
	}

	@Override
	public Collection<Avalia> consultarAluno(Long id) {
		Query query = manager.createQuery("SELECT a FROM Avalia a WHERE a.aluno.id = :id ");
		query.setParameter("id", id);

		return query.getResultList();
	}

	@Override
	public Collection<Avalia> consultarAlunoTarefa(Long idAluno, Long idTarefa) {
		Query query = manager.createQuery("SELECT a FROM Avalia a WHERE a.aluno.id = :idAluno "
				+ "AND a.postado.tarefa.id = :idTarefa");
		query.setParameter("idAluno", idAluno);
		query.setParameter("idTarefa", idTarefa);

		return query.getResultList();
	}

}
